package com.boluo.businessManagement.service.impl;

import java.util.List;

import com.boluo.businessManagement.dto.BusMentStoreDTO;
import com.boluo.businessManagement.service.IBusMentGoodsStoreService;
import com.boluo.common.constant.BusinessManagementConstants;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boluo.businessManagement.mapper.BusMentStoreMapper;
import com.boluo.businessManagement.domain.BusMentStore;
import com.boluo.businessManagement.service.IBusMentStoreService;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * 仓库信息Service业务层处理
 * 
 * @author boluo
 * @date 2023-05-02
 */
@Service
public class BusMentStoreServiceImpl implements IBusMentStoreService 
{
    @Resource
    private BusMentStoreMapper busMentStoreMapper;

    @Resource
    private IBusMentGoodsStoreService busMentGoodsStoreService;

    /**
     * 查询仓库信息
     * 
     * @param id 仓库信息主键
     * @return 仓库信息
     */
    @Override
    public BusMentStore selectBusMentStoreById(Long id)
    {
        return busMentStoreMapper.selectBusMentStoreById(id);
    }

    /**
     * 查询仓库信息列表
     * 
     * @param busMentStoreDTO 仓库信息
     * @return 仓库信息
     */
    @Override
    public List<BusMentStore> selectBusMentStoreList(BusMentStoreDTO busMentStoreDTO)
    {
        return busMentStoreMapper.selectBusMentStoreList(busMentStoreDTO);
    }

    /**
     * 新增仓库信息
     * 
     * @param busMentStore 仓库信息
     * @return 结果
     */
    @Override
    public int insertBusMentStore(BusMentStore busMentStore)
    {
        // 校验参数
        Assert.hasLength(busMentStore.getStoreName(),"仓库名不能为空");
        Assert.hasLength(busMentStore.getStoreAddress(),"仓库地址不能为空");
        int nameLen = busMentStore.getStoreName().length();
        int addLen = busMentStore.getStoreAddress().length();
        Assert.state(nameLen <= BusinessManagementConstants.STRING_NUMBER_FIFTY,"仓库名称不得超过50个字");
        Assert.state(addLen <= BusinessManagementConstants.STRING_NUMBER_TWO_HUNDRED_FIFTY,"仓库地址不得过250字");
        // 设置创库为正常
        busMentStore.setState(BusinessManagementConstants.BUS_MANAGEMENT_STORE_STATE_YES);
        return busMentStoreMapper.insertBusMentStore(busMentStore);
    }

    /**
     * 修改仓库信息
     * 
     * @param busMentStore 仓库信息
     * @return 结果
     */
    @Override
    public int updateBusMentStore(BusMentStore busMentStore)
    {

        // 校验参数
        Long busMentStoreId = busMentStore.getId();
        Assert.notNull(busMentStoreId,"你是什么东西?");
        Assert.hasLength(busMentStore.getStoreName(),"仓库名不能为空");
        Assert.hasLength(busMentStore.getStoreAddress(),"仓库地址不能为空");
        int nameLen = busMentStore.getStoreName().length();
        int addLen = busMentStore.getStoreAddress().length();
        Assert.state(nameLen <= BusinessManagementConstants.STRING_NUMBER_FIFTY,"仓库名称不得超过50个字");
        Assert.state(addLen <= BusinessManagementConstants.STRING_NUMBER_TWO_HUNDRED_FIFTY,"仓库地址不得过250字");
        // 校验状态
        Assert.state(busMentStore.getState()== BusinessManagementConstants.BUS_MANAGEMENT_STORE_STATE_YES,"该仓库已是删除状态不能进行修改");



        return busMentStoreMapper.updateBusMentStore(busMentStore);
    }

    /**
     * 批量删除仓库信息
     * 
     * @param id 需要删除的仓库信息主键
     * @return 结果
     */
    @Override
    public int deleteBusMentStoreByIds(Long id)
    {
        // 校验参数
        Assert.notNull(id,"仓库Id 不能为空");
        BusMentStore busMentStore = busMentStoreMapper.selectBusMentStoreById(id);
        Assert.notNull(busMentStore,"仓库Id不存在");
        // 校验状态
        Assert.state(busMentStore.getState()== BusinessManagementConstants.BUS_MANAGEMENT_STORE_STATE_YES,"该仓库已是删除状态不能进行删除");
        // 校验该仓库是否有数据有的话不能进行删除
        int count = busMentGoodsStoreService.countByStoreId(id);
        Assert.state(count==BusinessManagementConstants.NUMBER_ZERO,"该仓库已经有产品了,不能进行删除");

        // 设置仓库状态为删除状态
        busMentStore.setState(BusinessManagementConstants.BUS_MANAGEMENT_STORE_STATE_DEL);
        return busMentStoreMapper.updateBusMentStore(busMentStore);
    }

    /**
     * 删除仓库信息信息
     * 
     * @param id 仓库信息主键
     * @return 结果
     */
    @Override
    public int deleteBusMentStoreById(Long id)
    {
        return busMentStoreMapper.deleteBusMentStoreById(id);
    }
}
